﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;

namespace YJKArch.LeadToLeader
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	[Journaling(JournalingMode.NoCommandData)]
	public class Helper
	{
		public static string ReadTextMaxLenth(System.Windows.Forms.TextBox tb)
		{
			string result = null;
			int num = 0;
			string[] lines = tb.Lines;
			for (int i = 0; i < lines.Length; i++)
			{
				int num2 = Encoding.Default.GetBytes(lines[i]).Length;
				if (num2 > num)
				{
					num = num2;
					result = lines[i].ToString();
				}
			}
			return result;
		}

		public static XYZ Rotate(XYZ vect, XYZ axis, double angle)
		{
			double x = axis.X;
			double y = axis.Y;
			double z = axis.Z;
			double[,] array = new double[3, 3];
			array[0, 0] = x * x;
			array[0, 1] = x * y;
			array[0, 2] = x * z;
			array[1, 0] = y * x;
			array[1, 1] = y * y;
			array[1, 2] = y * z;
			array[2, 0] = z * x;
			array[2, 1] = z * y;
			array[2, 2] = z * z;
			double[,] array2 = array;
			double[,] array3 = new double[3, 3];
			array3[0, 1] = -z;
			array3[0, 2] = y;
			array3[1, 0] = z;
			array3[1, 2] = -x;
			array3[2, 0] = -y;
			array3[2, 1] = x;
			double[,] array4 = array3;
			double[,] array5 = new double[3, 3];
			array5[0, 0] = 1.0 - array2[0, 0];
			array5[0, 1] = -array2[0, 1];
			array5[0, 2] = -array2[0, 2];
			array5[1, 0] = -array2[1, 0];
			array5[1, 1] = 1.0 - array2[1, 1];
			array5[1, 2] = -array2[1, 2];
			array5[2, 0] = -array2[2, 0];
			array5[2, 1] = -array2[2, 1];
			array5[2, 2] = 1.0 - array2[2, 2];
			double[,] array6 = array5;
			double[,] array7 = new double[3, 3];
			array7[0, 0] = array2[0, 0] + Math.Cos(angle) * array6[0, 0] + Math.Sin(angle) * array4[0, 0];
			array7[0, 1] = array2[0, 1] + Math.Cos(angle) * array6[0, 1] + Math.Sin(angle) * array4[0, 1];
			array7[0, 2] = array2[0, 2] + Math.Cos(angle) * array6[0, 2] + Math.Sin(angle) * array4[0, 2];
			array7[1, 0] = array2[1, 0] + Math.Cos(angle) * array6[1, 0] + Math.Sin(angle) * array4[1, 0];
			array7[1, 1] = array2[1, 1] + Math.Cos(angle) * array6[1, 1] + Math.Sin(angle) * array4[1, 1];
			array7[1, 2] = array2[1, 2] + Math.Cos(angle) * array6[1, 2] + Math.Sin(angle) * array4[1, 2];
			array7[2, 0] = array2[2, 0] + Math.Cos(angle) * array6[2, 0] + Math.Sin(angle) * array4[2, 0];
			array7[2, 1] = array2[2, 1] + Math.Cos(angle) * array6[2, 1] + Math.Sin(angle) * array4[2, 1];
			array7[2, 2] = array2[2, 2] + Math.Cos(angle) * array6[2, 2] + Math.Sin(angle) * array4[2, 2];
			double[,] array8 = array7;
			return new XYZ(array8[0, 0] * vect.X + array8[0, 1] * vect.Y + array8[0, 2] * vect.Z, array8[1, 0] * vect.X + array8[1, 1] * vect.Y + array8[1, 2] * vect.Z, array8[2, 0] * vect.X + array8[2, 1] * vect.Y + array8[2, 2] * vect.Z);
		}

		public static void SetWitdth(TextNote textNote)
		{
			double num = textNote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * 100.0;
			double num2 = textNote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			byte[] bytes = Encoding.Default.GetBytes(textNote.Text);
			double num3 = num * num2;
			double num4 = (double)bytes.Length * num3 / 1.7;
			textNote.Width = num4 + 1.5;
		}

		public static double FontSize(TextNoteType textNoteType)
		{
			double num = textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * 100.0;
			double num2 = textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			return num * num2;
		}

		public static List<string> GetSystemFamilyElementList<T>(Autodesk.Revit.DB.Document doc)
		{
			List<string> list = new List<string>();
			try
			{
				foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(T)).ToElements())
				{
					if (element is T)
					{
						list.Add(element.Name);
					}
				}
			}
			catch (Exception ex)
			{
				ex.Message.ToString();
			}
			return list;
		}

		public static Element GetSelectType<T>(Autodesk.Revit.DB.Document doc, string selectTypeStr)
		{
			try
			{
				foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(T)).ToElements())
				{
					if (element is T && element.Name.StartsWith(selectTypeStr))
					{
						return element;
					}
				}
			}
			catch (Exception ex)
			{
				ex.Message.ToString();
			}
			return null;
		}

		public static void TxtSetNum(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == ' ')
			{
				e.KeyChar = '\0';
			}
			if (e.KeyChar == '-' && ((System.Windows.Forms.ComboBox)sender).Text.Length == 0)
			{
				return;
			}
			if (e.KeyChar > ' ')
			{
				try
				{
					double.Parse(((System.Windows.Forms.ComboBox)sender).Text + e.KeyChar.ToString());
				}
				catch
				{
					e.KeyChar = '\0';
				}
			}
		}

		public static void AddCombItems(List<string> levelList, System.Windows.Forms.ComboBox cb)
		{
			if (levelList != null)
			{
				foreach (string item in levelList)
				{
					cb.Items.Add(item);
				}
				if (cb.Items.Count > 0)
				{
					cb.SelectedIndex = 0;
				}
			}
		}

		public static List<string> GetConduitTypeStr(Autodesk.Revit.DB.Document doc, BuiltInCategory category)
		{
			List<string> result;
			try
			{
				List<string> list = new List<string>();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(ConduitType));
				filteredElementCollector.OfCategory(category);
				foreach (Element element in filteredElementCollector.ToElements())
				{
					list.Add(element.Name);
				}
				result = list;
			}
			catch (Exception ex)
			{
				ex.Message.ToString();
				result = null;
			}
			return result;
		}

		public const string ENDALIGN = "末端对齐";

		public const string MIDDLEALIGN = "居中对齐";

		public const string STARTALIGN = "始端对齐";
	}
}
